<?php 

class View
{
    protected $var=array();
    protected $controller;
    protected $_action;

    function __construct($controller,$action)
    {
        $this->controller=$controller;
        $this->action=$action;
    }

    //分配变量
    public function assign($name,$value)
    {
        $this->var[$name]=$value;
    }

    //在模板中调用php函数
    public function general()
    {
        $args=func_get_args();
        $general=new GeneralController("General",$args[0]);
        $func=array_shift($args);
        return $general->$func($args);
    }

    public function render($append)
    {
        extract($this->var);
        if ($append) {
            $defaultHeader=APP_PATH.'application/views/header.php';
            $defaultFooter=APP_PATH.'application/views/footer.php';
            $controllerHeader=APP_PATH.'application/views/'.$this->controller.'/header.php';
            $controllerFooter=APP_PATH.'application/views/'.$this->controller.'/footer.php';
            if (file_exists($controllerHeader)) {
                include $controllerHeader;
            }else{
                include $defaultHeader;
            }

            
        }

        $controllerLayout = APP_PATH . 'application/views/' . $this->controller . '/' . $this->action . '.php';
        if (file_exists($controllerLayout)) {
            include $controllerLayout;
        }else{
            include "./404.html";
        }

        if ($append) {
            if (file_exists($controllerFooter)) {
                include $controllerFooter;
            }else{
                include $defaultFooter;
            }
        }
    }

    
}